﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NK.Mvc3.Models;
using NK.Mvc3.DBL;
using NK.Mvc3.ViewModels;

namespace NK.Mvc3.Controllers
{ 
    public class SaleController : Controller
    {
        private StoreContext db = new StoreContext();

        //
        // GET: /Sale/

        public ViewResult Index()
        {
            return View(db.Sales.ToList());
        }

        //
        // GET: /Sale/Details/5

        public ViewResult Details(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        public ActionResult Grid(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        public ActionResult GridData(int id)
        {
            Sale sale = db.Sales.Find(id);
            return PartialView(sale);
        }

        public ActionResult GridItem(SaleDetail saleDetail)
        {
            return PartialView(saleDetail);
        }

        public ActionResult GridItemEdit(int id)
        {
            var saleDetail = db.SaleDetails.Include(i => i.Sale).Include(i => i.ProductInfo).Where(s => s.ID == id).Single();
            return PartialView(saleDetail);
        }

        public ActionResult CreateSaleDetail(int saleId)
        {
            //return "Some Text";
            //var sale = db.Sales.Find(saleId);
            //var saleEditVM = new SaleEditVM {Sale = sale};
            //return GridItemEdit(saleId);
            ViewBag.SaleID = saleId;
            return PartialView("GridItemEdit");
        }

        [HttpPost]
        public ActionResult CreateSaleDetail(SaleDetail saledetail)
        {
            if (ModelState.IsValid)
            {
                var sale = db.Sales.Find(saledetail.SaleID);
                if (sale!= null)
                {
                    sale.SaleDetails.Add(saledetail);
                    db.SaveChanges();
                    //model = db.SaleDetails.l
                    saledetail.ProductInfo = db.ProductInfos.Find(saledetail.ProductInfoID);
                    return PartialView("GridItem", saledetail);
                }
            }
            return PartialView("GridItemEdit", saledetail);
        }

        //
        // GET: /Sale/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Sale/Create

        [HttpPost]
        public ActionResult Create(Sale sale)
        {
            if (ModelState.IsValid)
            {
                db.Sales.Add(sale);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(sale);
        }
        
        //
        // GET: /Sale/Edit/5
 
        public ActionResult Edit(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        //
        // POST: /Sale/Edit/5

        [HttpPost]
        public ActionResult Edit(Sale sale)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sale).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(sale);
        }

        //
        // GET: /Sale/Delete/5
 
        public ActionResult Delete(int id)
        {
            Sale sale = db.Sales.Find(id);
            return View(sale);
        }

        //
        // POST: /Sale/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Sale sale = db.Sales.Find(id);
            db.Sales.Remove(sale);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}